﻿using Dapper;
using MQ.Core.Common;
using MQ.Core.Db;
using MQ.Core.Db.SqlServer;
using MQ.Core.Entity.System;
using MQ.Core.IRepository.System;
using MQ.Core.Util;
using System;
using System.Collections.Generic;
using System.Data;
using System.Text;

namespace MQ.Core.Repository.System
{
    public class CompanyRepository : ICompanyRepository
    {
        private readonly ISqlDb db;

        public CompanyRepository(ISqlDb db)
        {
            this.db = db;
        }
        public void Add(CompanyEntity entity)
        {
            //string sql = $"insert into sys_company(parentid,name,province,city,district,address,status,description) values(@parentid,@name,@province,@city,@district,@address,@status,@description)";
            //db.Execute(sql, entity);
            db.Insert(entity);
        }

        public void Delete(long id)
        {
            string sql = $"delete from sys_company where id={id}";
            db.Execute(sql);
        }

        public void Delete(string ids)
        {
            string sql = $"delete from sys_company where id in({ids})";
            db.Execute(sql);
        }

        public void Delete(List<long> ids)
        {
            Delete(ids.Join(","));
        }

        public IEnumerable<CompanyEntity> GetList(string query)
        {
            string sql = "select a.*,p.name ProvinceName,c.name CityName,d.name DistrictName from sys_company a left join sys_area p on p.code=a.province left join sys_area c on c.code=a.city left join sys_area d on d.code=a.district where 1=1";
            var json = query.ToJObject();
            if (!json["keyword"].IsEmpty())
            {
                sql += $" and (a.name like '%{json["keyword"].ToString()}%' or a.description like '%{json["keyword"].ToString()}%')";
            }
            if (!json["id"].IsEmpty())
            {
                sql += $" and a.id={json["id"].ToString()}";
            }
            if (!json["parentId"].IsEmpty())
            {
                sql += $" and a.parentId={json["parentId"].ToString()}";
            }
            return db.GetList<CompanyEntity>(sql);
        }

        public IEnumerable<CompanyEntity> GetPageList(Pagination pagination, string query)
        {
            string sql = "select a.*,p.name ProvinceName,c.name CityName,d.name DistrictName from sys_company a left join sys_area p on p.code=a.province left join sys_area c on c.code=a.city left join sys_area d on d.code=a.district where 1=1";
            var json = query.ToJObject();
            if (!json["keyword"].IsEmpty())
            {
                sql += $" and (a.name like '%{json["keyword"].ToString()}%' or a.description like '%{json["keyword"].ToString()}%')";
            }
            if (!json["id"].IsEmpty())
            {
                sql += $" and a.id={json["id"].ToString()}";
            }
            if (!json["parentId"].IsEmpty())
            {
                sql += $" and a.parentId={json["parentId"].ToString()}";
            }
            return db.GetList<CompanyEntity>(sql, pagination);
        }

        public void Update(CompanyEntity entity)
        {
            //string sql = $"update sys_company set parentid=@parentid,name=@name,province=@province,city=@city,district=@district,address=@address,status=@status,description=@description";
            //db.Execute(sql,entity);
            db.Update(entity);
        }
    }
}
